//
//  NetworkTools.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/3.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "NetworkTools.h"
#import "UserAccount.h"
#import "AFNetworking.h"



@interface NetworkTools ()

@end

@implementation NetworkTools

// 定义单例对象
+ (instancetype)sharedTools {
    static NetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL *url = [NSURL URLWithString:hostname];
        tools = [[NetworkTools alloc] initWithBaseURL:url];
        // 设置反序列化格式
        tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    return tools;
}

// 定义网络访问方法  以后所有的网络访问都经过该方法调用 AFN

// 加载字典数据
- (void)requestJsonDict: (HTTPMethod)method WithUrlString: (NSString *)urlString WithParameters: (NSMutableDictionary *)parameters WithFinished:(void(^)(NSDictionary *result, NSError *error))finished{

    // 调用 AFN 具体的 GET, POST 方法
    if (method == 1) {
        
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 校验数据是否是字典
            NSDictionary *dict = responseObject;
            if ([dict isKindOfClass:[NSDictionary class]]) {
                // 执行成功的回调
                finished(dict, nil);
                return ;
            }
            
            //数据格式不匹配
            //自定义错误信息
            //一般自定义errorcode  为负值
            NSError *dataError = [NSError errorWithDomain:weiboDomain code:-10000 userInfo:@{NSLocalizedDescriptionKey: @"数据错误"}];
            finished(nil, dataError);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            // 执行错误的回调
            finished(nil, error);
            NSLog(@"%@", error);
        }];
    }else{
        
        [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 校验数据是否是字典
            NSDictionary *dict = responseObject;
            if ([dict isKindOfClass:[NSDictionary class]]) {
                // 执行成功的回调
                finished(dict, nil);
                return ;
            }
            
            //数据格式不匹配
            //自定义错误信息
            //一般自定义errorcode  为负值
            NSError *dataError = [NSError errorWithDomain:weiboDomain code:-10000 userInfo:@{NSLocalizedDescriptionKey: @"数据错误"}];
            finished(nil, dataError);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            // 执行错误的回调
            finished(nil, error);
            NSLog(@"%@", error);
        }];
    }
}

//添加上传图片的方法

- (void)uploadImage: (NSString *)urlString WithParmaters: (NSMutableDictionary *)parmaters WithData: (NSData *)imageData WithFinished: (void(^)(NSDictionary *result, NSError *error))finished{
    [self POST:urlString parameters:parmaters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //1.data  要上传的文件的二进制数据
        //2.name 表示上传的文件服务器对应的字段
        //3.filename 表示服务器接受后 存储的名字   名字可以随便取
        //4.上传文件的格式  image/jpeg
        [formData appendPartWithFileData:imageData name:@"pic" fileName:@"OMG" mimeType:@"image/jpeg"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = responseObject;
        if (dict != nil) {
            finished(dict, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finished(nil, error);
        NSLog(@"uploadImage: error: %@", error);
    }];
}

@end
